package com.start.autumn.base;

import cn.hutool.core.collection.CollUtil;
import com.start.autumn.constant.ResponseCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

@Data
@ApiModel("接口统一返回对象")
public class ResponseBaseVO<T> {
    @ApiModelProperty("业务返回码")
    @NotNull
    private Integer code;

    @ApiModelProperty("业务返回提示消息")
    @NotEmpty
    private String message;

    @ApiModelProperty("业务返回数据")
    private T data;

    public ResponseBaseVO() {
        this.code = ResponseCodeEnum.OK.getCode();
    }

    public ResponseBaseVO(T data) {
        this.code = ResponseCodeEnum.OK.getCode();
        this.data = data;
    }

    public ResponseBaseVO(BaseEnum baseEnum) {
        if (Objects.nonNull(baseEnum)) {
            this.code = baseEnum.getCode();
            this.message = baseEnum.getErrMessage();
        } else {
            this.code = ResponseCodeEnum.OK.getCode();
        }
    }

    public ResponseBaseVO(BaseEnum baseEnum, T data) {
        if (Objects.nonNull(baseEnum)) {
            this.code = baseEnum.getCode();
            this.message = baseEnum.getErrMessage();
        }
        this.data = data;
    }

    public ResponseBaseVO(BaseEnum baseEnum, T data, String message) {
        if (Objects.nonNull(baseEnum)) {
            this.code = baseEnum.getCode();
        }
        this.data = data;
        this.message = message;
    }

    public ResponseBaseVO(BaseEnum baseEnum, String errorMessage) {
        if (Objects.nonNull(baseEnum)) {
            this.code = baseEnum.getCode();
        }
        this.message = errorMessage;
    }

    public static <T> ResponseBaseVO<T> ok() {
        return new ResponseBaseVO<>(ResponseCodeEnum.OK);
    }

    public static <T> ResponseBaseVO<T> ok(T data) {
        return new ResponseBaseVO<>(data);
    }

    public static <T> ResponseBaseVO<T> ok(BaseEnum baseEnum) {
        return new ResponseBaseVO<>(baseEnum);
    }

    public static <T> ResponseBaseVO<T> ok(BaseEnum baseEnum, T data) {
        return new ResponseBaseVO<>(baseEnum, data);
    }

    public static <T> ResponseBaseVO<T> fail(BaseEnum baseEnum) {
        return new ResponseBaseVO<>(baseEnum);
    }

    public static <T> ResponseBaseVO<T> fail(BaseEnum baseEnum, String message) {
        return new ResponseBaseVO<>(baseEnum, null, message);
    }

    public boolean isData() {
        if (data instanceof Collection) {
            Collection cn = (Collection) data;
            return CollUtil.isNotEmpty(cn);
        } else if (data instanceof Map) {
            Map map = (Map) data;
            return CollUtil.isNotEmpty(map);
        }
        return Objects.nonNull(data);
    }

    public boolean isSuccess() {
        return this.code.equals(ResponseCodeEnum.OK.getCode());
    }
}
